package com.lvyq.myplayer.ui.fragment

import android.view.View
import android.widget.Toast
import androidx.appcompat.widget.Toolbar
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lvyq.myplayer.R
import com.lvyq.myplayer.adapter.IndexUnifiedAdapter
import com.lvyq.myplayer.base.BaseFragment
import com.lvyq.myplayer.common.util.MockDataUtil
import com.lvyq.myplayer.model.index.IndexItem
import com.lvyq.myplayer.model.index.IndexLastSongModel
import com.lvyq.myplayer.model.index.IndexPickPlayLstModel
import com.lvyq.myplayer.model.index.IndexSectionTitle
import com.lvyq.myplayer.model.index.IndexHotSongModel
import com.lvyq.myplayer.model.index.IndexVideoModel
import com.lvyq.myplayer.util.ThreadUtil
import com.lvyq.myplayer.util.ToolBarManager

/**
 * 文件描述：IndexFragment
 * 作者：lvyq
 * 创建时间：2025-03-21
 * 修改时间：2025-03-21
 */
class IndexFragment: ToolBarManager, BaseFragment() {
    override lateinit var toolbar: Toolbar
    private lateinit var unifiedAdapter: IndexUnifiedAdapter

    lateinit var viewPickPlayLstModel: IndexPickPlayLstModel

    lateinit var viewIndexHotSongModel: IndexHotSongModel

    lateinit var viewIndexLastSongModel: IndexLastSongModel

    lateinit var viewIndexVideoModel:IndexVideoModel
    override fun initView(): View? {

        val view = View.inflate(context, R.layout.fragment_index, null)
        val recyclerView = view.findViewById<RecyclerView>(R.id.rVUnified)
        recyclerView.layoutManager = LinearLayoutManager(activity)

        toolbar=view.findViewById(R.id.toolBar)
        initMainToolBar()

        // 构造合并后的数据源
        val items = mutableListOf<IndexItem>()

        // 添加 banner
        items.add(IndexItem.Banner(  MockDataUtil.getIndexBannerList()))

        // 甄选歌单
        getPickPlayLst(items)


        //热门单曲
       // getHotSong(items)

        //最新单曲
         getLastSong(items)


        //推荐MV
       // getHotMV(items)

        //精选专辑
        //items.add(IndexItem.SectionTitle(IndexSectionTitle("最新专辑")))


        unifiedAdapter = IndexUnifiedAdapter(context, items)
        recyclerView.adapter = unifiedAdapter
        unifiedAdapter.setOnItemClickListener(object :IndexUnifiedAdapter.OnItemClickListener{
            override fun onItemClick(data: IndexSectionTitle, position: Int) {
                // 处理点击事件，
                ThreadUtil.runOnMainThread({
                    Toast.makeText(context, "点击了 【$position】${data.title}-更多", Toast.LENGTH_SHORT).show()
                })
            }

        })

        return view
    }

    /**
     * 精选MV
     */
    private fun getHotMV(items: MutableList<IndexItem>) {

        viewIndexVideoModel=ViewModelProvider(this)[IndexVideoModel::class.java]

        viewIndexVideoModel.data.observe(this){
                data->
            items.add(IndexItem.SectionTitle(IndexSectionTitle("推荐MV")))
            items.add(IndexItem.Video(data))

        }
        viewIndexVideoModel.getHotMvLst()
    }

    /**
     * 最新单曲
     */
    private fun getLastSong(items: MutableList<IndexItem>) {
        viewIndexLastSongModel=ViewModelProvider(this)[IndexLastSongModel::class.java]

        viewIndexLastSongModel.data.observe(this){
            data->
            items.add(IndexItem.SectionTitle(IndexSectionTitle("最新单曲")))
            items.add(IndexItem.LastSong(data.data))
        }
        viewIndexLastSongModel.getLastSongLst()
    }

    /**
     * 热门单曲
     */
    private fun getHotSong(items: MutableList<IndexItem>) {
        viewIndexHotSongModel=ViewModelProvider(this)[IndexHotSongModel::class.java]

        viewIndexHotSongModel.data.observe(this){data ->
            items.add(IndexItem.SectionTitle(IndexSectionTitle("热门单曲")))
            items.add(IndexItem.HotSong(data.data))
        }
        viewIndexHotSongModel.getHeatSongLst()
    }

    /**
     * 获取甄选歌单
     */
    private fun getPickPlayLst(items: MutableList<IndexItem>) {
        viewPickPlayLstModel=ViewModelProvider(this)[IndexPickPlayLstModel::class.java]
        //数据监听
        viewPickPlayLstModel.data.observe(this){ data ->
            items.add(IndexItem.SectionTitle(IndexSectionTitle("甄选歌单1.0.1")))
            items.add(IndexItem.GeDan(data.data))
        }
        //请求数据
        viewPickPlayLstModel.getPickPlayLst()

    }


}